package com.example.weather_news.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.weather_news.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 逝 on 2017/06/19.
 */

public class Adapter_Future extends BaseAdapter {

    private Context context;
    private JSONArray arrays=new JSONArray();

    public Adapter_Future(Context context) {
        this.context = context;
    }

    public void setData(JSONArray arrays) {
        this.arrays = arrays;
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return arrays.length();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.list_future, null);
            holder.tvFutureDate = (TextView) convertView.findViewById(R.id.tv_future_date);
            holder.ivFutureWeather = (ImageView) convertView.findViewById(R.id.iv_future_weather);
            holder.tvFutureWeather = (TextView) convertView.findViewById(R.id.tv_future_weather);
            holder.tvFutureTemp = (TextView) convertView.findViewById(R.id.tv_future_temp);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        JSONObject jo;
        try {
            jo = arrays.getJSONObject(position);
            String date = jo.getString("date");
            String week = jo.getString("week");

            String night = jo.getString("night");
            String templow=new JSONObject(night).getString("templow");

            String day = jo.getString("day");
            JSONObject object=new JSONObject(day);
            String weather=object.getString("weather");
            String temphigh=object.getString("temphigh");
            int img=object.getInt("img");

            setWeatherImg(holder.ivFutureWeather,img);
            holder.tvFutureWeather.setText(weather);
            holder.tvFutureTemp.setText(temphigh+"°~"+templow+"°");
            if (position==0){
                holder.tvFutureDate.setText("今   天"+"\t\t"+date.substring(5,10));
            }else {
                holder.tvFutureDate.setText(week+"\t\t"+date.substring(5,10));
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }


        return convertView;
    }

    //设置天气图标
    private void setWeatherImg(ImageView iv, int img){
        switch (img){
            case 0:
                iv.setBackgroundResource(R.mipmap.qing);
                break;
            case 1:
                iv.setBackgroundResource(R.mipmap.duoyun_bai);
                break;
            case 2:
                iv.setBackgroundResource(R.mipmap.yin);
                break;
            case 3:
                iv.setBackgroundResource(R.mipmap.zhenyu);
                break;
            case 4:
                iv.setBackgroundResource(R.mipmap.leizhenyu);
                break;
            case 5:
                iv.setBackgroundResource(R.mipmap.ic_launcher_round);
                break;
            case 6:
                iv.setBackgroundResource(R.mipmap.ic_launcher_round);
                break;
            case 7:
                iv.setBackgroundResource(R.mipmap.xiaoyu);
                break;
            case 8:
                iv.setBackgroundResource(R.mipmap.zhongyu);
                break;
            case 9:
                iv.setBackgroundResource(R.mipmap.dayu);
                break;
            case 10:
                iv.setBackgroundResource(R.mipmap.baoyu);
                break;
            case 22:
                iv.setBackgroundResource(R.mipmap.zhongyu);
                break;
            default:
                iv.setBackgroundResource(R.mipmap.ic_launcher_round);
                break;
        }
    }


    protected class ViewHolder {
        private TextView tvFutureDate;
        private ImageView ivFutureWeather;
        private TextView tvFutureWeather;
        private TextView tvFutureTemp;
    }
}
